<?php

    class Seatting_admin_m extends Model {

        function Seatting_admin_m() {
            parent::Model();
            $this->load->database();
        }

        function list_gardient($per_page, $offset) {
            $this->db->select('
            seating_gardient_account.gardient_name,
            seating_gardient_account.id,
            seating_gardient_profile.gardient_id,
            seating_gardient_profile.first_name,
            seating_gardient_profile.last_name,
            seating_gardient_profile.phone,
            seating_gardient_profile.address,
            seating_gardient_account.email,
            seating_gardient_profile.experience,
            seating_gardient_profile.available,
            seating_gardient_profile.dob,
            seating_gardient_profile.avatar,
            seating_gardient_profile.gender,
            animus_sub_region.name as city
            ');
            $this->db->from('seating_gardient_account,seating_gardient_profile,animus_sub_region');
            $this->db->where('seating_gardient_account.id = seating_gardient_profile.gardient_id');
            $this->db->where('animus_sub_region.id = seating_gardient_profile.city');
            $this->db->order_by('id', 'DESC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query->result_array();
        }

        function count_gardient() {
            $this->db->select('
            seating_gardient_account.gardient_name,
            seating_gardient_account.id,
            seating_gardient_profile.gardient_id,
            seating_gardient_profile.first_name,
            seating_gardient_profile.last_name,
            seating_gardient_profile.phone,
            seating_gardient_profile.address,
            seating_gardient_account.email,
            seating_gardient_profile.experience,
            seating_gardient_profile.available,
            seating_gardient_profile.dob,
            seating_gardient_profile.avatar,
            seating_gardient_profile.gender,
            animus_sub_region.name as city
            ');
            $this->db->from('seating_gardient_account,seating_gardient_profile,animus_sub_region');
            $this->db->where('seating_gardient_account.id = seating_gardient_profile.gardient_id');
            $this->db->where('animus_sub_region.id = seating_gardient_profile.city');
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return count($query->result_array());
        }

        function delete_gardient($id) {
            $this->db->delete('seating_gardient_profile', array('gardient_id' => $id));
            return $this->db->delete('seating_gardient_account', array('id' => $id));
        }

        function is_name_existe($name) {
            return $this->db->get_where('seating_gardient_account', array('gardient_name' => $name));
        }

        function get_exist_email($email){
            return $this->db->get_where('seating_gardient_account', array('email' => $email));
        }

        function add_gardient($data) {
            $query = $this->db->insert('seating_gardient_account', $data);
            return $query;
        }

        function get_account_id_max(){
            $this->db->select('MAX(id) as new_id');
            $query = $this->db->get('seating_gardient_account');
            return $query;
        }

        function add_gardient_profile($data) {
            $query = $this->db->insert('seating_gardient_profile', $data);
            return $query;
        }

        function get_detail_gardient($id) {
            $this->db->select('
            seating_gardient_account.gardient_name,
            seating_gardient_account.id,
            seating_gardient_profile.gardient_id,
            seating_gardient_profile.first_name,
            seating_gardient_profile.last_name,
            seating_gardient_profile.phone,
            seating_gardient_profile.address,
            seating_gardient_account.email,
            seating_gardient_profile.experience,
            seating_gardient_profile.available,
            seating_gardient_profile.dob,
            seating_gardient_profile.avatar,
            seating_gardient_profile.gender,
            animus_sub_region.name as city
            ');
            $this->db->from('seating_gardient_account,seating_gardient_profile,animus_sub_region');
            $this->db->where('seating_gardient_account.id = seating_gardient_profile.gardient_id');
            $this->db->where('seating_gardient_profile.city = animus_sub_region.id');
            $this->db->where('seating_gardient_account.id', $id);
            return $this->db->get();
        }

        function set_available_gardient($id) {
            return $this->db->update('seating_gardient_profile', array('available' => 1), array('gardient_id' => $id));
        }

        function set_unavailable_gardient($id) {
            return $this->db->update('seating_gardient_profile', array('available' => 0), array('gardient_id' => $id));
        }
        function search_gardient($name,$per_page, $offset) {
            if($name=='_'){
                $name = '';
            }
            $this->db->select('
            seating_gardient_account.gardient_name,
            seating_gardient_account.id,
            seating_gardient_profile.gardient_id,
            seating_gardient_profile.first_name,
            seating_gardient_profile.last_name,
            seating_gardient_profile.phone,
            seating_gardient_profile.address,
            seating_gardient_account.email,
            seating_gardient_profile.experience,
            seating_gardient_profile.available,
            seating_gardient_profile.dob,
            seating_gardient_profile.avatar,
            seating_gardient_profile.gender
            ');
            $this->db->from('seating_gardient_account,seating_gardient_profile');
            $this->db->where('seating_gardient_account.id = seating_gardient_profile.gardient_id');
            $this->db->like('seating_gardient_account.gardient_name', $name);
            $this->db->order_by('id', 'DESC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query->result_array();
        }
        function count_search_gardient($name) {
            if($name=='_'){
                $name = '';
            }
            $this->db->select('
            seating_gardient_account.gardient_name,
            seating_gardient_account.id,
            seating_gardient_profile.gardient_id,
            seating_gardient_profile.first_name,
            seating_gardient_profile.last_name,
            seating_gardient_profile.phone,
            seating_gardient_profile.address,
            seating_gardient_account.email,
            seating_gardient_profile.experience,
            seating_gardient_profile.available,
            seating_gardient_profile.dob,
            seating_gardient_profile.avatar,
            seating_gardient_profile.gender
            ');
            $this->db->from('seating_gardient_account,seating_gardient_profile');
            $this->db->where('seating_gardient_account.id = seating_gardient_profile.gardient_id');
            $this->db->like('seating_gardient_account.gardient_name', $name);
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return count($query->result_array());
        }

        //END GARDIENT

        //BEGIN TEXT
        function list_text($per_page, $offset) {
            $this->db->select();
            $this->db->from('seating_text');
            $this->db->order_by('id', 'DESC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query->result_array();
        }

        function count_text() {
            $this->db->select();
            $this->db->from('seating_text');
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return count($query->result_array());
        }

        function delete_text($id) {
            return $this->db->delete('seating_text', array('id' => $id));
        }

        function is_title_existe($id) {
            return $this->db->get_where('seating_text', array('id' => $id));
        }

        function add_text($data) {
            $query = $this->db->insert('seating_text', $data);
            return $query;
        }

        function get_detail_text($id) {
            $this->db->select();
            $this->db->from('seating_text');
            $this->db->where('id', $id);
            return $this->db->get();
        }

        function edit_text($data,$id) {
            return $this->db->update('seating_text', $data, array('id' => $id));
        }
        //END TEXT

        //BEGIN booking
        function get_booking($per_page, $off_set) {
            $this->db->select('
            seating_booking.id,
            seating_booking.title,
            seating_booking.status,
            seating_booking.contact_date,
            seating_booking.date_start,
            seating_gardient_account.gardient_name,
            lehu_profiles.display_name');
            $this->db->from('seating_booking ,lehu_profiles,seating_gardient_account');
            $this->db->where('seating_booking.user_id = lehu_profiles.user_id');
            $this->db->where('seating_gardient_account.id = seating_booking.gardient_id');
            $this->db->limit($per_page, $off_set);
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return $query->result_array();
        }

        function count_booking() {
            $this->db->select('
            seating_booking.id,
            seating_booking.title,
            seating_booking.contact_date,
            seating_gardient_account.gardient_name,
            lehu_profiles.display_name');
            $this->db->from('seating_booking ,lehu_profiles,seating_gardient_account');
            $this->db->where('seating_booking.user_id = lehu_profiles.user_id');
            $this->db->where('seating_gardient_account.id = seating_booking.gardient_id');
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return count($query->result_array());
        }

        function get_booking_by_id($id) {
            $this->db->select('
            lehu_users.email as email_user,
            lehu_profiles.display_name,
            lehu_profiles.first_name as first_name_user,
            lehu_profiles.last_name as last_name_user,
            lehu_profiles.company,
            lehu_profiles.dob as dob_user,
            lehu_profiles.gender as gender_user,
            lehu_profiles.phone as phone_user,
            lehu_profiles.mobile as mobile_user,
            lehu_profiles.address as address_user,
            lehu_profiles.gravatar,
            seating_gardient_account.gardient_name,
            seating_gardient_profile.first_name as first_name_gardient,
            seating_gardient_profile.last_name as last_name_gardient,
            seating_gardient_profile.phone as phone_gardient,
            seating_gardient_profile.address as address_gardient,
            seating_gardient_account.email as email_gardient,
            seating_gardient_profile.experience,
            seating_gardient_profile.available,
            seating_gardient_profile.dob as dob_gardient,
            seating_gardient_profile.avatar,
            seating_gardient_profile.gender as gender_gardient,
            animus_sub_region.name as city,
            seating_booking.id,
            seating_booking.title,
            seating_booking.content,
             seating_booking.status,
            seating_booking.contact_date,
            seating_booking.date_start,
            seating_booking.hour_start,
            seating_booking.total_date,
            seating_booking.total_hour,
            seating_booking.animal_kind,
            seating_booking.total_animal,
            seating_booking.medicaments,
            seating_booking.habitat,
            seating_booking.known');
            $this->db->from('
            lehu_users ,
            lehu_profiles ,
            seating_booking ,
            seating_gardient_account ,
            seating_gardient_profile,
            animus_sub_region');
            $this->db->where('lehu_users.id = lehu_profiles.user_id');
            $this->db->where('lehu_profiles.user_id = seating_booking.user_id');
            $this->db->where('seating_booking.gardient_id = seating_gardient_profile.gardient_id');
            $this->db->where('seating_gardient_account.id = seating_gardient_profile.gardient_id');
            $this->db->where('seating_gardient_profile.city = animus_sub_region.id');
            $this->db->where('seating_booking.id',$id);
            $query = $this->db->get();
            return $query;
        }

        function delete_booking($id) {
            return $this->db->delete('seating_booking', array('id' => $id));
        }

        //END booking
        //begin contact
        function get_contact($per_page, $off_set) {
            $this->db->select('
            seating_contact.id,
            seating_contact.title,
            seating_contact.date,
            seating_contact.date_start,
            seating_contact.hour_start,
            animus_sub_region.name as city,
            lehu_profiles.display_name');
            $this->db->from('seating_contact ,lehu_profiles,animus_sub_region');
            $this->db->where('seating_contact.user_id = lehu_profiles.user_id');
            $this->db->where('seating_contact.city = animus_sub_region.id');
            $this->db->limit($per_page, $off_set);
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return $query->result_array();
        }

        function count_contact() {
            $this->db->select('
            seating_contact.id,
            seating_contact.title,
            seating_contact.date,
            lehu_profiles.display_name');
            $this->db->from('seating_contact ,lehu_profiles,animus_sub_region');
            $this->db->where('seating_contact.user_id = lehu_profiles.user_id');
            $this->db->where('seating_contact.city = animus_sub_region.id');
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return count($query->result_array());
        }

        function get_contact_by_id($id) {
            $this->db->select('
            lehu_users.email as email_user,
            lehu_profiles.display_name,
            lehu_profiles.first_name as first_name_user,
            lehu_profiles.last_name as last_name_user,
            lehu_profiles.company,
            lehu_profiles.dob as dob_user,
            lehu_profiles.gender as gender_user,
            lehu_profiles.phone as phone_user,
            lehu_profiles.mobile as mobile_user,
            lehu_profiles.address as address_user,
            lehu_profiles.gravatar,
            seating_contact.id,
            seating_contact.title,
            seating_contact.content,
            seating_contact.date_start,
            seating_contact.total_date,
            seating_contact.total_hour,
            seating_contact.hour_start,
            seating_contact.animal_kind,
            seating_contact.total_animal,
            seating_contact.medicaments,
            seating_contact.habitat,
            seating_contact.fidelity,
            seating_contact.known,
            seating_contact.add_fidelity,
            animus_sub_region.name as city,
            seating_contact.date');
            $this->db->from('
            lehu_users ,
            lehu_profiles ,
            seating_contact,
            animus_sub_region');
            $this->db->where('lehu_users.id = lehu_profiles.user_id');
            $this->db->where('lehu_profiles.user_id = seating_contact.user_id');
            $this->db->where('seating_contact.city = animus_sub_region.id');
            $this->db->where('seating_contact.id',$id);
            $query = $this->db->get();
            return $query;
        }

        function delete_contact($id) {
            return $this->db->delete('seating_contact', array('id' => $id));
        }
        //end contact
        //BEGIN RECOMMANDATION
        function get_feedback($per_page, $offset) {
            $this->db->select('
            seating_feedback.id,
            seating_feedback.user_id,
            seating_feedback.title,
            seating_feedback.content,
            seating_feedback.feedback_date,
            seating_feedback.active,
            lehu_profiles.display_name
            ');
            $this->db->from('seating_feedback ,lehu_profiles');
            $this->db->where('lehu_profiles.user_id = seating_feedback.user_id');
            $this->db->order_by('id', 'DESC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query->result_array();
        }

        function count_feedback() {
            $this->db->select('
            seating_feedback.id,
            seating_feedback.user_id,
            seating_feedback.title,
            seating_feedback.content,
            seating_feedback.feedback_date,
            seating_feedback.active,
            lehu_profiles.display_name
            ');
            $this->db->from('seating_feedback ,lehu_profiles');
            $this->db->where('lehu_profiles.user_id = seating_feedback.user_id');
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return count($query->result_array());
        }

        function set_inactive($id) {
            return $this->db->update('seating_feedback', array('active' => 0), array('id' => $id));
        }

        function set_active($id) {
            return $this->db->update('seating_feedback', array('active' => 1), array('id' => $id));
        }

        function get_feedback_by_id() {
            $this->db->select('
            seating_feedback.id,
            seating_feedback.user_id,
            seating_feedback.title,
            seating_feedback.content,
            seating_feedback.feedback_date,
            seating_feedback.active,
            lehu_profiles.display_name
            ');
            $this->db->from('seating_feedback ,lehu_profiles');
            $this->db->where('lehu_profiles.user_id = seating_feedback.user_id');
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return $query;
        }

        function delete_feedback($id) {
            return $this->db->delete('seating_feedback', array('id' => $id));
        }
        //END COMMENDATION

        //BEGIN BLOG
        function get_blog($per_page, $offset) {
            $this->db->select('
            seating_blog.id,
            seating_blog.user_id,
            seating_blog.name,
            seating_blog.description,
            seating_blog.blog_date,
            seating_blog.image,
            lehu_profiles.display_name
            ');
            $this->db->from('seating_blog ,lehu_profiles');
            $this->db->where('lehu_profiles.user_id = seating_blog.user_id');
            $this->db->order_by('id', 'DESC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query->result_array();
        }

        function get_blog_by_id($id,$per_page, $offset) {
            $this->db->select('
            seating_blog.id,
            seating_blog.user_id,
            seating_blog.name,
            seating_blog.description,
            seating_blog.blog_date,
            seating_blog.image,
            lehu_profiles.display_name
            ');
            $this->db->from('seating_blog ,lehu_profiles');
            $this->db->where('lehu_profiles.user_id = seating_blog.user_id');
            $this->db->where('seating_blog.id',$id);
            $this->db->order_by('id', 'DESC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query;
        }

        function count_blog() {
            $this->db->select('
            seating_blog.id,
            seating_blog.user_id,
            seating_blog.name,
            seating_blog.description,
            seating_blog.blog_date,
            seating_blog.image,
            lehu_profiles.display_name
            ');
            $this->db->from('seating_blog ,lehu_profiles');
            $this->db->where('lehu_profiles.user_id = seating_blog.user_id');
            $this->db->order_by('id', 'DESC');
            $query = $this->db->get();
            return count($query->result_array());
        }

        function delete_blog($blog_id){
            return $this->db->delete('seating_blog', array('id' => $blog_id));
        }

        function delete_blog_comment($blog_id){
            return $this->db->delete('seating_comment', array('blog_id' => $blog_id));
        }
        //END BLOG
    }

?>